package cn.kgc.springboot.vue.service.impl;

import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.entity.User;
import cn.kgc.springboot.vue.mapper.UserMapper;
import cn.kgc.springboot.vue.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springboot.vue.entity.Dept;
import cn.kgc.springboot.vue.service.DeptService;
import cn.kgc.springboot.vue.mapper.DeptMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author mengshun
* @description 针对表【sys_dept】的数据库操作Service实现
* @createDate 2024-12-27 08:46:49
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService{

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseResult deptPage(QueryCondition<Dept> queryCondition) {

        Page<Dept> deptPage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        LambdaQueryWrapper<Dept> lambda = new QueryWrapper<Dept>().lambda();
        String deptName = queryCondition.getSearchParams().getDeptName();
        lambda.like(StringUtils.isNotBlank(deptName),Dept::getDeptName,deptName)
                .orderByDesc(Dept::getCreateTime);

        baseMapper.selectPage(deptPage,lambda);
        return ResponseResult.success().data("total",deptPage.getTotal())
                .data("tableData",deptPage.getRecords());
    }

    @Override
    public ResponseResult deleteDept(Integer id) {

        LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
        lambda.eq(User::getDeptId,id);

        List<User> users = userMapper.selectList(lambda);
        if (ObjectUtils.isNotNull(users)){
            return  ResponseResult.fail().message("部门被占用,不能删除");
        }

        baseMapper.deleteById(id);
        return ResponseResult.success();
    }
}




